package com.ruoyi.portal.service.impl;

import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.utils.DateUtils;
import com.ruoyi.common.core.utils.SecurityUtils;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.omp.api.RemoteCustomerService;
import com.ruoyi.omp.api.domain.ZsCustomer;
import com.ruoyi.portal.domain.PorConsulting;
import com.ruoyi.portal.mapper.PorConsultingMapper;
import com.ruoyi.portal.service.IPorConsultingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 预约咨询Service业务层处理
 *
 * @author cjie
 * @date 2021-06-20
 */
@Service
public class PorConsultingServiceImpl implements IPorConsultingService {
    @Autowired
    private PorConsultingMapper porConsultingMapper;

    @Autowired
    private RemoteCustomerService remoteCustomerService;

    /**
     * 查询预约咨询
     *
     * @param id 预约咨询ID
     * @return 预约咨询
     */
    @Override
    public PorConsulting selectPorConsultingById(Long id) {
        return porConsultingMapper.selectPorConsultingById(id);
    }

    /**
     * 查询预约咨询列表
     *
     * @param porConsulting 预约咨询
     * @return 预约咨询
     */
    @Override
    public List<PorConsulting> selectPorConsultingList(PorConsulting porConsulting) {
        return porConsultingMapper.selectPorConsultingList(porConsulting);
    }

    /**
     * 新增预约咨询
     *
     * @param porConsulting 预约咨询
     * @return 结果
     */
    @Override
    public int insertPorConsulting(PorConsulting porConsulting) {
        porConsulting.setCreateTime(DateUtils.getNowDate());
        return porConsultingMapper.insertPorConsulting(porConsulting);
    }

    /**
     * 修改预约咨询
     *
     * @param porConsulting 预约咨询
     * @return 结果
     */
    @Override
    public int updatePorConsulting(PorConsulting porConsulting) {
        porConsulting.setUpdateTime(DateUtils.getNowDate());
        return porConsultingMapper.updatePorConsulting(porConsulting);
    }

    /**
     * 批量删除预约咨询
     *
     * @param ids 需要删除的预约咨询ID
     * @return 结果
     */
    @Override
    public int deletePorConsultingByIds(Long[] ids) {
        return porConsultingMapper.deletePorConsultingByIds(ids);
    }

    /**
     * 删除预约咨询信息
     *
     * @param id 预约咨询ID
     * @return 结果
     */
    @Override
    public int deletePorConsultingById(Long id) {
        return porConsultingMapper.deletePorConsultingById(id);
    }

    @Override
    public AjaxResult syncPorConsulting(String id) {
        PorConsulting porConsulting = selectPorConsultingById(Long.parseLong(id));
        //调用远程接口，保存数据。
        ZsCustomer zsCustomer = new ZsCustomer();
        zsCustomer.setContactPhone(porConsulting.getPhone());
        zsCustomer.setCustomerSource("0");//门户
        zsCustomer.setEnterpriseContact(porConsulting.getLiaison());
        zsCustomer.setEnterpriseName(porConsulting.getEnterprise());
        R result = remoteCustomerService.addCustomer(zsCustomer);
        if(result.getCode() == R.SUCCESS){
            // 远程保存成功后，修改状态。
            porConsulting.setState("1");
            updatePorConsulting(porConsulting);
            return AjaxResult.success();
        }
        //失败
        return AjaxResult.error(result.getCode(),result.getMsg());

    }
}
